#include<graphics.h>
#include<time.h>
#include<conio.h>

#define MAXSTAR 800	//星星最大值
#define WIDTH 640	//图表宽度
#define HEIGHT 480	//图表高度
//星星封装结构体
struct STAR
{
	int x, y;
	double step;//用于星星移动速度
	int color;
};
STAR star[MAXSTAR];
//星星初始化
void InitStart(int i)
{
	star[i].x = 0;
	star[i].y = rand() % HEIGHT;
	star[i].step = (rand()% 5000) / 1000.0 + 1;
	star[i].color = (int)(star[i].step * 255 / 6.0 + 0.5);//速度越快，星星越亮
	star[i].color = RGB(star[i].color, star[i].color, star[i].color);
}
//星星移动
void MoveStar(int i)
{
	putpixel(star[i].x, star[i].y, 0);

	star[i].x += star[i].step;
	if (star[i].x >= WIDTH)
		InitStart(i);
	putpixel(star[i].x, star[i].y, star[i].color);
}
int main(void)
{
	srand((unsigned)time(NULL));//播种随机种子
	initgraph(WIDTH, HEIGHT);//初始化图形

	for (int i = 0; i < MAXSTAR; i++)//遍历初始全部星星
	{
		InitStart(i);
		star[i].x = rand() % WIDTH;
	}
	//星星移动
	while (!_kbhit())//按任意键退出
	{
		for (int i = 0; i < MAXSTAR; i++)
			MoveStar(i);
		Sleep(20);
	}
	system("pause");
	closegraph();
	return 0;
}